package com.jmx.yp.struts.member;

import net.sf.json.JSONObject;
import java.util.*;
import org.apache.log4j.*;

import com.jmx.yp.common.SysConstants;
import com.jmx.yp.exception.*;
import com.jmx.yp.service.member.ICategoryService;
import com.jmx.yp.entity.EmpCategory;
import com.jmx.yp.entity.GroupEmployee;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * 功能描述：
 *			处理会员分类的业务控制类
 * @author Jenny
 *
 * Date:2011-2-15 下午04:16:36
 */
public class CategoryAction {
	
	private static Logger log = (Logger)Logger.getLogger(CategoryAction.class);
	
	private ICategoryService categoryService;
	
	public void setCategoryService(ICategoryService categoryService){
		this.categoryService = categoryService;
	}
	
	/**
	 * 查询会员级别：入口
	 * @return
	 */
	public String initCategoryList(){
		return "queryCategory";
	}
	
	/**
	 * 查询会员级别
	 * @return
	 */
	public String queryCategory(){
		try{
			Map map = categoryService.queryCategory(queryName, nowPage);
			
			result = JSONObject.fromObject(map);

		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction queryCategory error: " + e.getMessage());
		}
		
		return "success";
	}
	
	/**
	 * 新增会员级别：入口
	 * @return
	 */
	public String initAddCategory(){
		return "addCategory";
	}
	
	/**
	 * 新增会员级别
	 * @return
	 */
	public String addCategory(){
		try{
			EmpCategory category = assembleEmpCategory();
			categoryService.addCategory(category);
			
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction addCatetory error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	/**
	 * 装配会员级别
	 * @return
	 */
	private EmpCategory assembleEmpCategory(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		
		EmpCategory category = new EmpCategory();
		if(serialid != null)
			category.setSerialid(Long.parseLong(serialid));
		category.setName(name.trim());
		category.setDescription(description.trim());
		category.setState(SysConstants.STATE_NORMAL);
		category.setOperator(employee.getGrpemp_id());
		category.setOperateTime(new java.util.Date());
		category.setInvalid(SysConstants.VALID);
		
		return category;
	}
	
	/**
	 * 会员类别详情
	 * @return
	 */
	public String detailCategory(){
		String forward = "error";
		try{
			EmpCategory category = categoryService.getEmpCategory(Long.parseLong(serialid));
			if(category != null)
				setProperties(category);
			
			gradeList =  categoryService.queryGradeByCategoryId(Long.parseLong(serialid));
			if(gradeList ==null || gradeList.isEmpty())
				gradeList = null;
			
			forward = "detailCategory";
		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction detailCategory error: " + e.getMessage());
		}
		return forward;
	}
	
	/**
	 * 设置属性值
	 * @param category
	 */
	private void setProperties(EmpCategory category){
		serialid = category.getSerialid().toString();
		name = category.getName();
		description = category.getDescription();
	}
	/**
	 * 修改会员类别：入口
	 * @return
	 */
	public String initUpdateCategory(){
		String forward = "error";
		try{
			EmpCategory category = categoryService.getEmpCategory(Long.parseLong(serialid));
			if(category != null)
				setProperties(category);
			forward = "updateCategory";
		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction detailCategory error: " + e.getMessage());
		}
		
		return forward;
	}
	
	/**
	 * 修改会员类别信息
	 * @return
	 */
	public String updateCategory(){
		try{
			EmpCategory category = assembleEmpCategory();
			category.setSerialid(Long.parseLong(serialid));
			
			categoryService.updateCategory(category);
			
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction updateCategory error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	public String deleteCategory(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		try{
			categoryService.deleteCategory(serialid, employee.getGrpemp_id());
			
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("CategoryAction deleteCategory error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	/* -- 属性值 -- */
	private String results;
	
	private String queryName;
	private int nowPage;
	
	private String serialid;
	private String name;
	private String description;
	
	private JSONObject result; //保留
	
	private List gradeList;
	

	public List getGradeList(){
		return gradeList;
	}
	
	public void setGradeList(List gradeList){
		this.gradeList = gradeList;
	}
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getResults() {
		return results;
	}

	public void setResults(String results) {
		this.results = results;
	}

	public String getQueryName() {
		return queryName;
	}

	public void setQueryName(String queryName) {
		this.queryName = queryName;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public String getSerialid() {
		return serialid;
	}

	public void setSerialid(String serialid) {
		this.serialid = serialid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
}
